package com.cuit.usercenter.controller;


import com.cuit.usercenter.entity.UcenterMember;
import com.cuit.usercenter.entity.vo.RegisterVo;
import com.cuit.usercenter.service.UcenterMemberService;
import com.cuit.utils.JwtUtils;
import com.cuit.utils.R;
import com.cuit.utils.ordervo.UcenterMemberOrder;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.HttpRequestHandler;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author horizon
 * @since 2020-09-23
 */
@RestController
@RequestMapping("/ucenter/member")
@CrossOrigin
public class UcenterMemberController {
    @Autowired
    private UcenterMemberService ucenterMemberService;

    //登录
    @PostMapping("login")
    public R userLogin(@RequestBody UcenterMember ucenterMember){
        //调用service方法实现登录
        //返回token值，使用jwt生成
        String token = ucenterMemberService.login(ucenterMember);
        return R.ok().data("token",token);
    }

    //注册
    @PostMapping("register")
    public R registerUser(@RequestBody RegisterVo registerVo){
        ucenterMemberService.register(registerVo);
        return R.ok();
    }

    //根据token获取用户信息
    @GetMapping("getUserInfo")
    public R getUserInfo(HttpServletRequest request){
        //调用jwt工具类中的方法，根据request对象获取头信息，返回用户id
        String userId = JwtUtils.getMemberIdByJwtToken(request);
        //根据userId查询数据库
        UcenterMember user = ucenterMemberService.getById(userId);
        return R.ok().data("userInfo",user);
    }

    //根据用户id获取用户信息---15内容新加
    @PostMapping("getUserInfoOrder/{id}")
    public UcenterMemberOrder getUserInfoOrder(@PathVariable String  id){
        UcenterMember member = ucenterMemberService.getById(id);
        //把memeber对象里面值复制给UcenterMemberOrder对象
        UcenterMemberOrder ucenterMemberOrder = new UcenterMemberOrder();
        BeanUtils.copyProperties(member,ucenterMemberOrder);
        return ucenterMemberOrder;
    }
}

